package com.ifast.sys.controller;

import com.ifast.common.annotation.Log;
import com.ifast.common.base.AdminBaseController;
import com.ifast.common.domain.Tree;
import com.ifast.common.type.EnumErrorCode;
import com.ifast.common.utils.MD5;
import com.ifast.common.utils.Result;
import com.ifast.common.utils.ShiroUtils;
import com.ifast.sys.domain.MenuDO;
import com.ifast.sys.domain.UserDO;
import com.ifast.sys.service.MenuService;
import com.ifast.sys.service.UnitBasicinfoService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <pre>
 * </pre>
 * <small> 2018年3月23日 | Aron</small>
 */
@Controller
public class LoginController extends AdminBaseController {
    @Value("${sysName}")
    private String sysName ;

    @Autowired
    MenuService menuService;

    @Autowired
    UnitBasicinfoService unitBasicinfoService;

    @GetMapping({ "/", "" })
    @Log("重定向到登录")
    String welcome(Model model) {
        return "redirect:/login_index";
    }

    @GetMapping("/login_{loginfrom}")
    String login(Model model,@PathVariable("loginfrom") String loginfrom) {
        model.addAttribute("sysName", sysName);
        model.addAttribute("loginfrom", loginfrom);
        return "login";
    }

    @Log("登录")
    @PostMapping("/login")
    @ResponseBody
    Result<String> ajaxLogin(String username, String password,String loginfrom) {
        password = MD5.MD5Encode(password);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            //getUser().setLoginFrom(loginfrom);
            getUser().setLoginFrom("curSys");
            getUser().setUnit(unitBasicinfoService.selectById(getUser().getUnitid()));
            return Result.ok();
        } catch (AuthenticationException e) {
            return Result.build(EnumErrorCode.userLoginFail.getCode(), EnumErrorCode.userLoginFail.getMsg());
        }
    }

    @Log("请求访问主页")
    @GetMapping({ "/index" })
    String index(Model model) {
        UserDO user = getUser();
        List<Tree<MenuDO>> menus = menuService.listMenuTree(user.getId());
        model.addAttribute("menus", menus);
        model.addAttribute("name", user.getName());
        model.addAttribute("username",user.getUsername());
        if(StringUtils.isBlank(user.getPicLocation()) ){
            model.addAttribute("picUrl","/img/photo_s.jpg");
        }else{
            model.addAttribute("picUrl", "/sys/user/IoReadImage/upload?name="+user.getPicLocation());
        }
        model.addAttribute("sysName", sysName);
        return "index";
    }

    @GetMapping("/logout")
    @Log("退出")
    String logout(Model model) {
        ShiroUtils.logout();
        return "redirect:/login"+"_index";
    }
    
    @Log("主页")
    @GetMapping("/main")
    String main() {
        return "main/main";
    }

    @Log("错误403")
    @GetMapping("/403")
    String error403() {
        return "403";
    }

}
